<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use ReflectionFunction;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Indent extends Backend
{

    /**
     * Indent模型对象
     * @var \app\admin\model\Indent
     */
    protected $model = null;


//    protected $noNeedRight = ['table1', 'table2', 'table3'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Indent;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $status = json_decode($this->request->request('filter'),true);
            $map = [];
            if(!isset($status['status']) || !$status['status']){
                $map['indent.status'] = ['>',0];
            }else{
                $map['indent.status'] = ['=',$status['status']];
            }
            
            $func = new ReflectionFunction($where);
            $where = $func->getStaticVariables();
            foreach($where['where'] as $key => $val)
            {
                if($val[0] == "indent.status")
                {
                    unset($where['where'][$key]);
                }
                if($val[0] == "indent.name")
                {
                    $map['indent.name'] = ['like','%'.$val[2].'%'];
                }
                if($val[0] == "indent.type")
                {
                    $map['indent.type'] = ['=',$val[2]];
                }
                if($val[0] == "indent.number")
                {
                    $map['indent.number'] = ['like','%'.$val[2].'%'];
                }
                if($val[0] == "indent.create_time")
                {
                    $map['indent.create_time'] = ['BETWEEN TIME',$val[2]];
                }
                if($val[0] == "indent.username")
                {
                    $map['indent.username'] = ['like','%'.$val[2].'%'];
                }
                if($val[0] == "address.phone")
                {
                    $map['address.phone'] = ['like','%'.$val[2].'%'];
                }
            }

            // $list = $this->model
            //         ->with(['address'])
            //         ->where($where)
            //         ->order($sort, $order)
            //         ->paginate($limit);

            $list = $this->model
                ->with(['address'])
                ->where($map)
                ->where('pid','<>',1)
                // ->where('pid','<=',1)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                // $row->visible(['id','order_id','transaction_id','user_id','username','name','title','number','address_id','goods_id','item','num','price','money','paidin','type','cou_id','status','pid','desc','pay_time','order_time','create_time','apply','oddnumbers','express','ex_time','logistics_time','is_state','number']);
    //             $row->visible(['address']);
				// $row->getRelation('address')->visible(['name','phone','address']);

                switch($row['status'])
                {
                    case 1;
                        $row['status'] = '待支付';
                        break;
                    case 2;
                        $row['status'] = '待发货';
                        break;
                    case 3;
                        $row['status'] = '待收货';
                        break;
                    case 4;
                        $row['status'] = '退款中';
                        break;
                    case 5;
                        $row['status'] = '已完成';
                        break;
                    case 6;
                        $row['status'] = '已退款';
                        break;
                    case 7;
                        $row['status'] = '已取消';
                        break;
                }
                switch($row['type'])
                {
                    case 1;
                        $row['type'] = '余额支付';
                        break;
                    case 2;
                        $row['type'] = '微信支付';
                        break;
                    case 3;
                        $row['type'] = '爱心值';
                        break;
                }
                if($row['pid'] != 0)
                {
                    $row['money'] = $row['money'] > 1 ? $row['money'] * $row['num'].'.00' : $row['money'];
                }
                // if($row['pid'] != 0)$row['money'] = $row['money'] * $row['num'].'.00';
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('dd',1);
        return $this->view->fetch();
    }



    /*
     * 订单详情
     */
    public function details($ids = "")
    {
        $row = $this->model->get(['id' => $ids])->toArray();

        switch($row['status'])
        {
            case 1;
                $row['status'] = "待支付";
                break;
            case 2;
                $row['status'] = "已支付";
                break;
            case 3;
                $row['status'] = "待发货";
                break;
            case 4;
                $row['status'] = "退款中";
                break;
            case 5;
                $row['status'] = "已完成";
                break;
            case 6;
                $row['status'] = "已退款";
                break;
            case 7;
                $row['status'] = "已取消";
                break;
        }
        switch($row['type'])
        {
            case 1;
                $row['type'] = "余额支付";
                break;
            case 2;
                $row['type'] = "微信支付";
                break;
            case 3;
                $row['type'] = "积分";
                break;
        }
        $url = config('fastadmin.api_url');
        if($row['pid'] != 0)
        {
            $goods = Db::name('goods')->field('id,image')->column('id,image');
            $address=Db::name('user_address')->field('id,address')->column('id,address');
            $child = Db::name('order')->where('pid',$row['pid'])->field('goods_id,address_id,money,name,title')->select();
            if($child)
            {
                foreach($child as $key => $val)
                {
                    $goods[$val['goods_id']] ? $child[$key]['img'] = $url.$goods[$val['goods_id']] : $child[$key]['img'] = "";
                    $address[$val['address_id']] ? $child[$key]['address'] = $address[$val['address_id']] : $child[$key]['address'] = "";
                }
            }
            $row['child'] = $child;
        }else{
            $img = Db::name('goods')->where('id',$row['goods_id'])->value('image');
            $row['img'] = $img ? $url.$img : "";
            $row['child'] = "";
        }
        if($row['pid'] != 0)$row['money'] = $row['money'] * $row['num'];
        $address = Db::name('user_address')->where('id',$row['address_id'])->field('phone,provincial,city,area,address')->find();
        $row['address'] = $address['provincial'].' '.$address['city'].' '.$address['area'].' '.$address['address'];
        $row['phone'] = $address['phone'];
        $this->view->assign("row", $row);
        return $this->view->fetch();

    }
    
    
    /*一键发货*/
    public function multi($ids = "")
    {
        
        $data = json_decode(json_encode($this->model->where('id','in',$ids)->field('id,status')->select()),true);
        
        if(count($data))
        {
            foreach($data as $key => $val)
            {
                if($val['status'] != 2)$this->error('该状态不能确认发货');
            }
        }
        
        $res = $this->model->where('id','in',$ids)->update(['status'=>3]);
        
        if($res)
        {
            $this->success('一键发货成功');
        }else{
            $this->error('一键发货失败');
        }
        
    }

}
